package com.nbjtjc.safe.service.emg.handle;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleGoodsMapper;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleListMapper;
import com.nbjtjc.safe.mapper.emg.handle.EmgHandleMapper;
import com.nbjtjc.safe.model.emg.handle.EmgHandle;
import com.nbjtjc.safe.model.sys.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EmgHandleService {

    @Autowired
    private EmgHandleMapper emgHandleMapper;

    @Autowired
    private EmgHandleListMapper emgHandleListMapper;

    @Autowired
    private EmgHandleGoodsMapper emgHandleGoodsMapper;

    public Result find(EmgHandle emgHandle) {
        Result result = new Result();
        PageHelper.startPage(emgHandle.getPageCurrent(), emgHandle.getPageSize());
        List<EmgHandle> emgHandles = emgHandleMapper.search(emgHandle);
        if (CollectionUtil.isEmpty(emgHandles)) {
            result.setData(new PageInfo<>(new ArrayList<>()));
        } else {
            result.setData(new PageInfo<>(emgHandles));
        }
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result update(EmgHandle emgHandle) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandle) || ObjectUtil.isNull(emgHandle.getTypeId())
                || ObjectUtil.isNull(emgHandle.getName())
                || ObjectUtil.isNull(emgHandle.getId())) {
            result.setFalse("关键字段的数据不能为空");
            return result;
        }
        int res = 0;
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        emgHandle.setDeptId(sysUser.getDeptId().intValue());
        emgHandleListMapper.deleteByHandleId(emgHandle.getId());
        emgHandleGoodsMapper.deleteByHandleId(emgHandle.getId());
        res = emgHandleMapper.updateByPrimaryKeySelective(emgHandle);
        if (res == 1) {
            addHandleList(emgHandle);
            addHandleGoods(emgHandle);
        }
        if (res != 1) {
            result.setFalse("插入或者更新失败");
            return result;
        }
        result.setData(emgHandle);
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result add(EmgHandle emgHandle) {
        Result result = new Result();
        if (ObjectUtil.isNull(emgHandle) || ObjectUtil.isNull(emgHandle.getTopTypeId())
                || ObjectUtil.isNull(emgHandle.getName())) {
            result.setFalse("关键字段的数据不能为空");
            return result;
        }
        if(ObjectUtil.isNull(emgHandle.getTypeId())||emgHandle.getTypeId().equals(0)){
            emgHandle.setTypeId(emgHandle.getTopTypeId());
        }
        int res = 0;
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("user");
        emgHandle.setDeptId(sysUser.getDeptId().intValue());
        emgHandle.setCompanyId(sysUser.getCompanyId());
        emgHandle.setCreateTime(new Date());
        res = emgHandleMapper.insertSelective(emgHandle);
        if (res == 1) {
            addHandleList(emgHandle);
            addHandleGoods(emgHandle);
        }
        if (res != 1) {
            result.setFalse("插入或者更新失败");
            return result;
        }
        result.setData(emgHandle);
        return result;
    }

    @Transactional(rollbackFor = {Throwable.class})
    public Result delete(Integer hId) {
        Result result = new Result();
        if (ObjectUtil.isNull(hId) || hId < 1) {
            result.setFalse("删除的主键id不能为空");
            return result;
        }
        int res = emgHandleMapper.deleteByPrimaryKey(hId);
        if (res == 0) {
            result.setFalse("删除失败");
            return result;
        }
        return result;
    }

    private void addHandleList(EmgHandle emgHandle) {
        if (CollectionUtil.isNotEmpty(emgHandle.getEmgHandleLists())) {
            emgHandle.getEmgHandleLists().forEach(emgHandleList -> {
                emgHandleList.setHandleId(emgHandle.getId());
            });
            emgHandleListMapper.insertList(emgHandle.getEmgHandleLists());
        }
    }

    private void addHandleGoods(EmgHandle emgHandle) {
        if (CollectionUtil.isNotEmpty(emgHandle.getEmgHandleGoodsList())) {
            emgHandle.getEmgHandleGoodsList().forEach(emgHandleGoods -> {
                emgHandleGoods.setHandleId(emgHandle.getId());
            });
            emgHandleGoodsMapper.insertList(emgHandle.getEmgHandleGoodsList());
        }
    }

}
